package com.appkubes.mall.restful.application.goods.goods.dao;

import com.appkubes.mall.core.dao.BaseMallGoodsDao;
import com.appkubes.mall.core.entity.MallGoods;
import com.appkubes.mall.restful.application.goods.goods.entity.model.GoodsIndexResult;
import com.appkubes.mall.restful.application.goods.goods.entity.model.GoodsUserModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface GoodsDao extends BaseMallGoodsDao {

    @Select({"SELECT * FROM `mall_goods` " +
            " WHERE  ${queryStr} " +
            " ORDER BY ${orderBy} " +
            "LIMIT #{page},#{limit}"})
    List<MallGoods> getList(@Param("queryStr") String queryStr,
                            @Param("orderBy") String orderBy,
                            @Param("page") int page,
                            @Param("limit") int limit
    );

    @Select({"SELECT * FROM `mall_goods` " +
            " WHERE  ${queryStr} " +
            " ORDER BY ${orderBy} " +
            "LIMIT #{page},#{limit}"})
    List<GoodsIndexResult> getSaasGoodses(@Param("queryStr") String queryStr,
                                          @Param("orderBy") String orderBy,
                                          @Param("page") int page,
                                          @Param("limit") int limit
    );


    @Select({"SELECT DISTINCT `t2`.`img`,`t1`.`create_time` FROM `mall_order_goods` `t1` INNER JOIN `mall_user` `t2` ON " +
            "`t2`.`user_key`=`t1`.`user_key` WHERE `t1`.`goods_key`=#{goodsKey} ORDER BY `t1`.`create_time` DESC LIMIT 17  "})
    List<GoodsUserModel> getGoodsUserModel(@Param("goodsKey") String goodsKey);

    @Select({"SELECT COUNT(*) AS tp_count FROM ( SELECT count(*) AS think_count FROM `mall_order_goods` WHERE `goods_key`=#{goodsKey} " +
            "GROUP BY `user_key` ) `_group_count_`"})
    int getOrderGoodsCount(@Param("goodsKey") String goodsKey);


}